package com.dongdongshop.controller;

import com.dongdongshop.domain.AjaxResult;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.TemplateService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("templateController")
public class TemplateController {

    @DubboReference
    private TemplateService templateService;

    //查询全部模板信息
    @RequestMapping("templateList")
    @ResponseBody
    public AjaxResult<List<TbTypeTemplate>> templateList(String name){
         List<TbTypeTemplate>list = templateService.getTemplateList(name);
        return AjaxResult.OK().setData(list);
    }

    //增加 模板信息
    @RequestMapping("addTypeTemplate")
    @ResponseBody
    public AjaxResult addTypeTemplate(TbTypeTemplate typeTemplate){
        if (typeTemplate == null){
            return AjaxResult.R(ResultEnum.PARAMETER_EMPTY);
        }
        try {
            templateService.addTypeTemplate(typeTemplate);
            return AjaxResult.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }

    }

    //回显信息
    @RequestMapping("getTypeTemplateById")
    @ResponseBody
    public AjaxResult<TbTypeTemplate> getTypeTemplateById(Long id){
        if (id == null ||  id.intValue() <= 0){
            return AjaxResult.R(ResultEnum.PARAMETER_EMPTY);
        }
        try {
            TbTypeTemplate typeTemplate = templateService.getTypeTemplateById(id);
            return AjaxResult.OK().setData(typeTemplate);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //修改模板信息
    @RequestMapping("updateTypeTemplate")
    @ResponseBody
    public AjaxResult updateTypeTemplate(TbTypeTemplate typeTemplate){
        if (typeTemplate == null){
            return AjaxResult.R(ResultEnum.PARAMETER_EMPTY);
        }
        try {
            templateService.updateTypeTemplate(typeTemplate);
            return AjaxResult.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //删除模板信息
    @RequestMapping("deleteTypeTemplate")
    @ResponseBody
    public AjaxResult deleteTypeTemplate(Long [] ids){
        if (ids.length == 0){
            return AjaxResult.R(ResultEnum.PARAMETER_EMPTY);
        }
        try {
            templateService.deleteTypeTemplate(ids);
            return AjaxResult.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }
}
